<?php
	
	define('SPECIFIC', 'Specific Holidays and Shopper Missions');
	define('SPECIFIC_ABBR', 'Seasonal Missions');
	
	include_once('../config.php'); 
	LinkDataBase();
	
	?>
	<script type="text/javascript">
	<?php
	//Find Category.
	if ($_POST['type']==SPECIFIC_ABBR)
		$sql = "SELECT * FROM droplist WHERE year='".$_POST['year']."' AND season='".$_POST['season']."' AND name LIKE '%".SPECIFIC."%' OR name LIKE '%".SPECIFIC_ABBR."%'";
	else
		$sql = "SELECT * FROM droplist WHERE year='".$_POST['year']."' AND season='".$_POST['season']."' AND name NOT LIKE '%".SPECIFIC."%' AND name NOT LIKE '%".SPECIFIC_ABBR."%'";
		
	$result = mysql_query($sql);
	
	$index = 0;
	while($row = mysql_fetch_assoc($result)) {
		
		//Find table number in category.
		$sql = "SELECT table_name, sheet.table_number AS table_number, charttype, keyword, contain, x_axis, charttitle
		FROM sheetreveal_first 
		LEFT JOIN sheet ON sheet.table_number=sheetreveal_first.table_number 
		WHERE droplist_id = '".$row['id']."' AND sheet.season = '".$_POST['season']."'";
		
		$result_sheetreveal_first = mysql_query($sql);
		
		$index_detail = 0;
		if ($result_sheetreveal_first) {
			while($row_first = mysql_fetch_assoc($result_sheetreveal_first)) {
				
				$table_number = $row_first['table_number'];
				
				//Check category contain sub-category.
				if ($row['sub_name']==NULL) {
					
					$sheetreveal_first[$row['name']]['table_number'][] = $table_number;
					$sheetreveal_first[$row['name']]['table_name'][] = $row_first['table_name'];
					$sheetreveal_first[$row['name']]['charttitle'][] = $row_first['charttitle'];
				}
				else {
					
					$Title_ContainSub[$row['name']][$row['sub_name']]['table_number'][] = $table_number;
					$Title_ContainSub[$row['name']][$row['sub_name']]['table_name'][] = $row_first['table_name'];
					$Title_ContainSub[$row['name']][$row['sub_name']]['charttitle'][] = $row_first['charttitle'];
				}
				$hrefID = 'ac-'.$index;
				?>
					
				var href = '<?php echo $hrefID; ?>';
				var ChartType = '#<?php echo $row_first['charttype']; ?>';
				
				//Process keyword.
				var keyword = '';
				<?php
				if (isset($row_first['keyword'])) {
				?>
					keyword = '<?php echo $row_first['keyword']; ?>';
				<?php
				}
				?>
						
				//Process contain.
				var contain = '';
				<?php
				if (isset($row_first['contain'])) {
				?>
					contain = '<?php echo $row_first['contain']; ?>';
				<?php
				}
				?>
	
				//Process Xaxis.
				var Xaxis = '';
				<?php
				if (isset($row_first['x_axis'])) {
				?>
					Xaxis = '<?php echo $row_first['x_axis']; ?>';
				<?php
				}
				?>
				
				//Change href.
				$('.ac-container article#'+href+' a#href-<?php echo $index_detail; ?>').attr('href','#<?php echo $table_number; ?>'+ChartType+'#'+keyword+'#'+contain+'#'+Xaxis);
				<?php
				$index_detail++;
			}
		}
		$index++;
	}
?>
	function GotoDetail(href, title) {
					
		var year = $("#year-droplist").text();
		var season = $("#season-droplist").text();
		var type = '<?php echo $_POST['type']; ?>';
		var Array_Href = $(href).attr('href').split('#');
		$("#content-detail").load('../chart/detail/index.php',
		{'year':year, 'season':season, 'type':type, 'tablename': Array_Href[1], 'charttype': Array_Href[2], 
		'keyword': Array_Href[3], 'contain': Array_Href[4], 'Xaxis': Array_Href[5], 'step': 1});
	}
</script>
<div class="container">
	<?php
	if ($_POST['type']==SPECIFIC_ABBR) {
		$Title = key($Title_ContainSub);
	?>
		<header>
			<h2><?php echo $Title; ?></h2>
		</header>
		<section class="ac-container">
			
			<?php
			//Search second stage.
			for ($sub_index=0;$sub_index<count($Title_ContainSub[$Title]);$sub_index++) {
				$Array_Key = array_keys($Title_ContainSub[$Title]);
				$id_index = $sub_index;
			?>
				<div>
					<input id="ac-<?php echo $id_index; ?>" name="accordion-<?php echo $id_index; ?>" type="checkbox"  />
					<label for="ac-<?php echo $id_index; ?>"><?php echo $Array_Key[$sub_index]; ?></label>
					<article class="ac-small" id="ac-<?php echo $id_index; ?>">
						<ul>
						<?php 
						
						//Search third stage.
						for ($index_detail=0;$index_detail<count($Title_ContainSub[$Title][$Array_Key[$sub_index]]['table_number']);$index_detail++) {
						?>
							<a id="href-<?php echo $index_detail; ?>" href="#" onclick="GotoDetail(this);">
								<li>
									<?php echo $Title_ContainSub[$Title][$Array_Key[$sub_index]]['charttitle'][$index_detail]; ?>
								</li>
							</a>
						<?php
						}
						?>
						</ul>
					</article>
				</div>
			<?php	
			}
			?>                
		</section>
	<?php
	}
	else {
	?>
	  <header>
		  <h1>TABLE OF CONTENTS</h1>
	  </header>
	  <section class="ac-container">
	  
	  <?php 
	  $index = 0;
	  //Search first stage.
	  while ((is_array($sheetreveal_first))&&(current($sheetreveal_first))) {
		  
		  $Title = key($sheetreveal_first);
	  ?>
		  <div>
			  <input id="ac-<?php echo $index; ?>" name="accordion-<?php echo $index; ?>" type="checkbox"/>
			  <label for="ac-<?php echo $index; ?>"><?php echo $Title; ?></label>
		  
			  <article class="ac-small" id="ac-<?php echo $index; ?>">
			  <ul>
			  <?php
			  
			  //Search second stage.
			  for ($index_detail=0;$index_detail<count($sheetreveal_first[$Title]['table_number']);$index_detail++) {
			  ?>
				  <a id="href-<?php echo $index_detail; ?>" href="#" onclick="GotoDetail(this);">
					  <li>
						  <?php echo $sheetreveal_first[$Title]['charttitle'][$index_detail]; ?>
					  </li>
				  </a>
			  <?php
			  }
			  ?>
			  </ul>
			  </article>
		  </div>
	  <?php
		  next($sheetreveal_first);
		  $index++;
	  }
	  ?>
	  
	  </section>
	
	<?php
	}
	?>
</div>
